package com.tuo51.childcare.controller;


import com.alibaba.fastjson.JSON;
import com.tuo51.childcare.domain.ptest.PTestCertificate;
import com.tuo51.childcare.domain.ptest.PTestItem;
import com.tuo51.childcare.domain.vo.PTestItemAnswerSheetItemVO;
import com.tuo51.childcare.service.PTestService;
import com.tuo51.childcare.service.QRCodeService;
import com.tuo51.evaluate.domain.childrens.Children;
import com.tuo51.core.domain.staffs.Staff;
import com.tuo51.evaluate.service.ChildrenService;
import com.tuo51.foudation.exception.BusinessException;
import com.tuo51.foudation.model.ErrorCode;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/papi/ptest")
@Api(value = "/papi/ptest", tags = "家长服务号 - 超级妈妈大比拼")
public class PAPIPtestController {

    @Autowired
    MemVerificationCache cache;

    @Autowired
    ChildrenService childrenService;

    @Autowired
    PTestService testService;

    @Autowired
    QRCodeService qrCodeService;

    @RequestMapping(value = "/today_tips", method = GET)
    public StatusResponse todayTips(@RequestHeader("ticket") String ticket,
                                    @RequestParam(value = "childrenId", required = false) String childrenId) {
        Staff parent = cache.getStaff(ticket);

        Children c = null;
        if (StringUtils.isNotEmpty(childrenId)) {
            c = childrenService.getById(parent.getId(), childrenId);
        }else{
            c = childrenService.defaultChildrenForParent(parent);
        }

        if (c == null){
            throw new BusinessException(ErrorCode.CHILDREN_NOT_EXIST, "没有指定宝宝");
        }

        Integer month = 13;
        Integer day = 376;

        if (c != null){
            month = DateUtil.betweenMonths(new Date(), c.getBirthday());
            day = DateUtil.betweenDay(new Date(), c.getBirthday());
        }

        List<PTestItem> ret = testService.getDailyTips(null, month, day );

        return StatusResponse.success(ret);
    }

    @RequestMapping(value = "/today_test", method = GET, produces = "application/json")
    public StatusResponse todayTest(@RequestHeader("ticket") String ticket,
                                    @RequestParam(value = "childrenId", required = false) String childrenId) {
        Staff parent = cache.getStaff(ticket);

        Children c = null;
        if (StringUtils.isNotEmpty(childrenId)) {
            c = childrenService.getById(parent.getId(), childrenId);
        }else{
            c = childrenService.defaultChildrenForParent(parent);
        }

        if (c == null){
            throw new BusinessException(ErrorCode.CHILDREN_NOT_EXIST, "没有指定宝宝");
        }

        Integer month = 13;
        Integer day = 377;

        if (c != null){
            month = DateUtil.betweenMonths(new Date(), c.getBirthday());
            day = DateUtil.betweenDay(new Date(), c.getBirthday());
        }

        List<PTestItem> ret = testService.getDailyTest(null, month, day );

        return StatusResponse.success(ret);
    }

    @RequestMapping(value = "/today_test_conclusion", method = POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticket", value = "票据", paramType = "header"),
            @ApiImplicitParam(name = "answerSheetJson", value = "答题卡", required = true, example = "JSON格式，如 [{'questionId':'1234', 'selections':[]}]"),
    })
    public StatusResponse todayTestConclusion(@RequestHeader("ticket") String ticket, String answerSheetJson) {
        //处理答题卡
        Staff parent = cache.getStaff(ticket);

        List<PTestItemAnswerSheetItemVO> answerSheet = JSON.parseArray(answerSheetJson, PTestItemAnswerSheetItemVO.class);

        //生成
        //增加得分
        PTestCertificate certificate = testService.parseTestAnswer(parent, null, null, answerSheet );

        //
        return StatusResponse.success(certificate);
    }

    @RequestMapping(value = "/certificate", method = GET)
    //允许匿名获取
    public StatusResponse certificateDetail(String certificateId ) {
        if (StringUtils.isEmpty(certificateId)) {
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "证书没找到");
        }



        return StatusResponse.success(testService.getPTestCertificate(certificateId));
    }

}
